package org.sgx.dao.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.sgx.dao.util.OrderedMap;

/**
 * misc utilities
 * @author sg
 *
 */
public class Util {
	public static Map<Object,Object> toMap(Object...a) {
		Map<Object,Object> m = new HashMap<Object,Object>();
		for (int i = 0; i < a.length-1; i=i+2) 
			m.put(a[i], a[i+1]);
		return m;
	}
	
//	public static OrderedMap<Object,Object> toOrderedMap(Object...a) {
//		OrderedMap<Object,Object> m = new OrderedMap<Object,Object>();
//		for (int i = 0; i < a.length-1; i=i+2) 
//			m.put(a[i], a[i+1]);
//		return m;
//	}
	
	public static <T> OrderedMap<T,T> toOrderedMap(T...a) {
		OrderedMap<T, T> m = new OrderedMap<T, T>();
		for (int i = 0; i < a.length-1; i=i+2) 
			m.put(a[i], a[i+1]);
		return m;
	}
	public static OrderedMap<Object,Object> OMap(Object ...a) {
		return toOrderedMap(a);
	}
//	public static <T extends OrderedMap<?,?>> T toOrderedMap(Object...a) {
//		OrderedMap<Object, Object> m = new OrderedMap<Object, Object>();
//		for (int i = 0; i < a.length-1; i=i+2) 
//			m.put(a[i], a[i+1]);
//		return (T) m;
//	}
	
	
	
	
	
	/* *** dates *** */
	private static SimpleDateFormat dateFormatDate, dateFormatDateTime, dateFormatTime;
	
	private static final String 
		dateFormatPatternDate="yyyy-MM-dd", 
		dateFormatPatternDateTime="yyyy-MM-dd H:mm:ss", 
		dateFormatPatternTime="H:mm:ss.SS";
	
	private static Locale dateFormatLocale;
	/**
	 * return the Locale used for date formatting. It is always en-US
	 */
	public static Locale getLocale() {
		if(dateFormatLocale==null)
			dateFormatLocale=new Locale("en","US");
		return dateFormatLocale;
	}	
	public static SimpleDateFormat getDateFormatDate() {
		if(dateFormatDate==null)
			dateFormatDate=new SimpleDateFormat(dateFormatPatternDate, getLocale());
		return dateFormatDate;
	}
	private static SimpleDateFormat getDateFormatDateTime() {
		if(dateFormatDateTime==null)
			dateFormatDateTime=new SimpleDateFormat(dateFormatPatternDateTime, getLocale());
		return dateFormatDateTime;
	}
	private static SimpleDateFormat getDateFormatTime() {
		if(dateFormatTime==null)
			dateFormatTime=new SimpleDateFormat(dateFormatPatternTime, getLocale());
		return dateFormatTime;
	}
	/**
	 * pasa a un string en formato aaaa-mm-dd
	 */
	public static String toDate(Date d) {
		return getDateFormatDate().format(d);
	}
	/**
	 * pasa a un string en formato hh:mm:ss
	 */
	public static String toTime(Date d) {
		return getDateFormatTime().format(d);
	}
	/**
	 * pasa a un string en formato aaaa-mm-dd hh:mm:ss
	 */
	public static String toDateTime(Date d) {
		return getDateFormatDateTime().format(d);
	}

	public static boolean dateEquals(Date d1, Date d2) {
		System.out.println(toDateTime(d1)+" - "+toDateTime(d2));
		return toDateTime(d1).equals(toDateTime(d2));
	}

	public static byte[] toByteArray(InputStream is) throws IOException {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		int nRead;
		byte[] data = new byte[16384];
		while ((nRead = is.read(data, 0, data.length)) != -1) {
		  buffer.write(data, 0, nRead);
		}
		buffer.flush();
		return buffer.toByteArray();
	}
	
}
